extends CenterContainer


onready var z_op = $PanelContainer/VBoxContainer/name/OptionButton

var edit_data = null


func _ready():
	pass


func set_edit_data(data):
	edit_data = data
	
	if edit_data:
		$PanelContainer/VBoxContainer/position/position.text = str("(",data.x, " ", data.y, ")")
		z_op.select(z_op.get_item_index(data["z"]))
		
		$PanelContainer/VBoxContainer/modalTitle.text = "编辑土壤"
	else:
		$PanelContainer/VBoxContainer/modalTitle.text = "添加土壤"
	

func state_update():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	var position_info = map_view.get_select_position_info()
	$PanelContainer/VBoxContainer/position/position.text = str("(",position_info.x, " ", position_info.y, ")")


func _on_ok_button_up():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	var position_info = map_view.get_select_position_info()
	
	var tree_list = DataSource.get_data_by_list(DataSource.map_trees, position_info["map_register"]["id"], "mapId")
	
	if edit_data:
		edit_data["z"] = z_op.get_selected_id()
		DataSource.update_data(DataSource.map_trees, edit_data)
		
		MainTool.toast_info("更新土壤成功!")
	else:
		var data = {
			"x": position_info.x,
			"y": position_info.y,
			"z": z_op.get_selected_id(),
			"mapId": position_info["map_register"]["id"]
		}
		for tree in tree_list:
			if tree["x"] == position_info.x and tree["y"] == position_info.y:
				MainTool.toast_info("该位置不可重复添加!")
				return
	
		DataSource.add_data(DataSource.map_trees, data)
		
		MainTool.toast_info("添加土壤成功!")
	
	map_view.entity_update()
	
	MainTool.getUILayer().pop_ui()


func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()
